﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;

    public class Allocation : BasePage
    {
        protected Button btnBatchAllocate;
        protected Button btnBatchPreAllocate;
        protected Button btnBatchRevertAllocate;
        protected Button btnBatchRevertPreAllocate;
        protected Button btnConfirmPick;
        protected Button btnGoBack;
        protected Button btnRefresh;
        protected Button btnRevertPick;
        protected Button btnSave;
        protected DropDownList ddlStatus;
        protected DropDownList ddlType;
        protected DropDownList ddlWareHouseID;
        protected HtmlForm F7041;
        protected GridView gvOrderDetail;
        protected GridView gvPickDetail;
        protected GridView gvPreallocateDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnOrdersID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected HtmlInputHidden hiddenFlag;
        protected HtmlInputButton htmlbtnAction;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblActualShipDate;
        protected Label lblASN;
        protected Label lblCCompany;
        protected Label lblConsigneeID;
        protected Label lblDeliverDate;
        protected Label lblExternalOrderID;
        protected Label lblExternalOrderID2;
        protected Label lblIntransitID;
        protected Label lblMsg;
        protected Label lblOrderGroup;
        protected Label lblOrdersDate;
        protected Label lblOrdersID;
        protected Label lblOwernID;
        protected Label lblPDTotal;
        protected Label lblPDTotalAllocated;
        protected Label lblPDTotalAllocatedQty;
        protected Label lblPDTotalPreallocated;
        protected Label lblPDTotalPreallocatedQty;
        protected Label lblPDTotalQty;
        protected Label lblPriority;
        protected Label lblReferenceNum;
        protected Label lblRequestedShipDate;
        protected Label lblStatus;
        protected Label lblTotalAllcated;
        protected Label lblTotalAllcatedQty;
        protected Label lblTotalOriginal;
        protected Label lblTotalOriginalQty;
        protected Label lblTotalPicked;
        protected Label lblTotalPickedQty;
        protected Label lblTotalPreallocated;
        protected Label lblTotalPreallocatedQty;
        protected Label lblTotalQty;
        protected Label lblTotalShipped;
        protected Label lblTotalShippedQty;
        protected Label lblTyep;
        protected Label lblWareHouseID;
        protected LinkButton lnkbtnAllocation;
        protected LinkButton lnkbtnCloseOrder;
        protected LinkButton lnkbtnDirectAllocation;
        protected LinkButton lnkbtnOwnerTransfer;
        protected LinkButton lnkbtnPreAllocation;
        protected LinkButton lnkbtnPrintKittingWork;
        protected LinkButton lnkbtnPrintOutbound;
        protected LinkButton lnkbtnPrintPick;
        protected LinkButton lnkbtnPrintPickList;
        protected LinkButton lnkbtnPrintPickWork;
        protected LinkButton lnkbtnReleasePickTasks;
        protected LinkButton lnkbtnRevertAllocation;
        protected LinkButton lnkbtnRevertPreAllocation;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected Label one4;
        private decimal PDTotalAllocatedQty;
        private decimal PDTotalPreallocatedQty;
        private decimal PDTotalQty;
        protected TextBox Qty;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnAllocation;
        protected HtmlTableCell tdlnkbtnCloseOrder;
        protected HtmlTableCell tdlnkbtnDirectAllocation;
        protected HtmlTableCell tdlnkbtnOwnerTransfer;
        protected HtmlTableCell tdlnkbtnPreAllocation;
        protected HtmlTableCell tdlnkbtnPrintKittingWork;
        protected HtmlTableCell tdlnkbtnPrintOutbound;
        protected HtmlTableCell tdlnkbtnPrintPick;
        protected HtmlTableCell tdlnkbtnPrintPickList;
        protected HtmlTableCell tdlnkbtnPrintPickWork;
        protected HtmlTableCell tdlnkbtnReleasePickTasks;
        protected HtmlTableCell tdlnkbtnRevertAllocation;
        protected HtmlTableCell tdlnkbtnRevertPreAllocation;
        private decimal TotalAllcatedQty;
        private decimal TotalOriginalQty;
        private decimal TotalPickedQty;
        private decimal TotalPreallocatedQty;
        private decimal TotalShippedQty;
        protected TextBox txtActualShipDate;
        protected TextBox txtCCompany;
        protected TextBox txtConsigneeID;
        protected TextBox txtDeliverDate;
        protected TextBox txtExternalOrderID;
        protected TextBox txtExternalOrderID2;
        protected TextBox txtIntransitID;
        protected TextBox txtOrderGroup;
        protected TextBox txtOrdersDate;
        protected TextBox txtOrdersID;
        protected TextBox txtOwernID;
        protected TextBox txtPriority;
        protected TextBox txtReferenceNum;
        protected TextBox txtRequestedShipDate;

        protected void btnBatchAllocate_Click(object sender, EventArgs e)
        {
            try
            {
                string orderDetailID = this.txtOrdersID.Text.Trim();
                List<string> lineID = base.GetSelectedIDsInGridView(this.gvOrderDetail, "LineID");
                if ((lineID == null) || (lineID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_016");
                }
                else
                {
                    string strMsg = string.Empty;
                    strMsg = OutboundServiceClientProxy.GetInstance().BatchAllocate(orderDetailID, lineID, base.UserInfo.UserID);
                    if (strMsg.EndsWith(","))
                    {
                        strMsg = strMsg.Substring(0, strMsg.Length - 1);
                    }
                    this.lblMsg.Text = strMsg;
                    this.SetOrderInfoForm();
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);$.unblockUI();</script>", false);
            }
        }

        protected void btnBatchPreAllocate_Click(object sender, EventArgs e)
        {
            try
            {
                string orderDetailID = this.txtOrdersID.Text.Trim();
                List<string> lineID = base.GetSelectedIDsInGridView(this.gvOrderDetail, "LineID");
                if ((lineID == null) || (lineID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_015");
                }
                else
                {
                    string strMsg = string.Empty;
                    strMsg = OutboundServiceClientProxy.GetInstance().BatchPreAllocate(orderDetailID, lineID, base.UserInfo.UserID);
                    if (!string.IsNullOrEmpty(strMsg))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F7400_004") + orderDetailID + strMsg;
                    }
                    else
                    {
                        this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_005"), orderDetailID);
                    }
                    this.SetOrderInfoForm();
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);$.unblockUI();</script>", false);
            }
        }

        protected void btnBatchRevertAllocate_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> pickDetailInfo = base.GetSelectedIDsInGridView(this.gvPickDetail, "PickDetailID");
                if ((pickDetailInfo == null) || (pickDetailInfo.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_018");
                }
                else
                {
                    string strMsg = OutboundServiceClientProxy.GetInstance().RevertAllocate(pickDetailInfo);
                    if (!string.IsNullOrEmpty(strMsg))
                    {
                        this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_019"), strMsg.Substring(0, strMsg.Length - 1));
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F7400_010");
                    }
                    this.SetOrderInfoForm();
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);$.unblockUI();</script>", false);
            }
        }

        protected void btnBatchRevertPreAllocate_Click(object sender, EventArgs e)
        {
            try
            {
                string orderDetailID = this.txtOrdersID.Text.Trim();
                List<string> lineID = base.GetSelectedIDsInGridView(this.gvOrderDetail, "LineID");
                if ((lineID == null) || (lineID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_017");
                }
                else
                {
                    string strMsg = string.Empty;
                    strMsg = OutboundServiceClientProxy.GetInstance().RevertPreAllocate(orderDetailID, lineID);
                    if (!string.IsNullOrEmpty(strMsg))
                    {
                        this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_007"), orderDetailID, strMsg.Substring(0, strMsg.Length - 1));
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F7400_008");
                    }
                    this.SetOrderInfoForm();
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);$.unblockUI();</script>", false);
            }
        }

        protected void btnConfirmPick_Click(object sernder, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PickDetailID");
                List<string> ToLoc = new List<string>();
                for (int i = 0; this.gvPickDetail.Rows.Count > i; i++)
                {
                    if (((CheckBox) this.gvPickDetail.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        if (!string.IsNullOrEmpty(((TextBox) this.gvPickDetail.Rows[i].FindControl("txtToLoc")).Text))
                        {
                            ToLoc.Add(((TextBox) this.gvPickDetail.Rows[i].FindControl("txtToLoc")).Text);
                        }
                        else
                        {
                            this.lblMsg.Text = "拣货单号: " + this.gvPickDetail.DataKeys[i]["PickDetailID"].ToString() + "请输入目标货位!";
                            return;
                        }
                    }
                }
                if ((selectedID != null) && (selectedID.Count > 0))
                {
                    string result = OutboundServiceClientProxy.GetInstance().ConfirmPicked(selectedID, ToLoc, base.UserInfo.UserID);
                    if (!string.IsNullOrEmpty(result))
                    {
                        this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_011"), result);
                        this.SetOrderInfoForm();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7400_012") + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);$.unblockUI();</script>", false);
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("AllocationQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("AllocationList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetOrderInfoForm();
                if (!string.IsNullOrEmpty(this.hdnRtnValue.Value))
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + this.hdnRtnValue.Value + ",4)</script>", false);
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
            }
        }

        protected void btnRevertPick_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PickDetailID");
                if ((selectedID != null) && (selectedID.Count > 0))
                {
                    string result = OutboundServiceClientProxy.GetInstance().RevertPicked(selectedID, base.UserInfo.UserID);
                    if (!string.IsNullOrEmpty(result))
                    {
                        this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_013"), result);
                        this.SetOrderInfoForm();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7400_014") + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);$.unblockUI();</script>", false);
            }
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string CS40000 = this.ddlType.SelectedValue.ToUpper();
            if ((CS40000 != null) && (CS40000 == "TRANSFER"))
            {
                this.ddlWareHouseID.Visible = true;
                this.lblWareHouseID.Visible = true;
                this.txtIntransitID.Visible = true;
                this.lblIntransitID.Visible = true;
                this.lblConsigneeID.Visible = false;
                this.txtConsigneeID.Visible = false;
                this.lblCCompany.Visible = false;
                this.txtCCompany.Visible = false;
            }
            else
            {
                this.ddlWareHouseID.Visible = false;
                this.lblWareHouseID.Visible = false;
                this.txtIntransitID.Visible = false;
                this.lblIntransitID.Visible = false;
                this.lblConsigneeID.Visible = true;
                this.txtConsigneeID.Visible = true;
                this.lblCCompany.Visible = true;
                this.txtCCompany.Visible = true;
            }
            this.ddlWareHouseID.Enabled = false;
        }

        protected void gvOrderDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7041", this.gvOrderDetail);
        }

        protected void gvOrderDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as OrdersInfo).OrderDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,4) </script>", false);
        }

        protected void gvOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvOrderDetail.PageIndex * this.gvOrderDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                string strStatus = this.gvOrderDetail.DataKeys[e.Row.RowIndex]["ExpiryType"].ToString();
                if (!(string.IsNullOrEmpty(base.Request.QueryString["type"]) || !(base.Request.QueryString["type"] == "QUERY")))
                {
                    ((LinkButton) e.Row.FindControl("lnkbtnPuraway")).Enabled = false;
                }
                else if (e.Row.FindControl("lblSkuID") != null)
                {
                    ((Label) e.Row.FindControl("lblSkuID")).Attributes.Add("onclick", "showInv('" + base.FormatUrl("../Inventory/InvLotLocLpnQuery.aspx?from=list&OwnerID={0}&SkuID={1}&History={2}", this.txtOwernID.Text.Trim(), ((Label) e.Row.FindControl("lblSkuID")).Text.Trim(), "Y") + "')");
                }
                string CS40001 = strStatus.ToUpper();
                if (CS40001 != null)
                {
                    if (!(CS40001 == "ABOUTTOEXPIRE"))
                    {
                        if (CS40001 == "EXPIRED")
                        {
                            e.Row.Cells[7].BackColor = Color.FromName("#ff0600");
                            e.Row.Cells[7].ToolTip = "有库存已经过期";
                        }
                    }
                    else
                    {
                        e.Row.Cells[7].BackColor = Color.FromName("#f86e00");
                        e.Row.Cells[7].ToolTip = "有库存即将过期";
                    }
                }
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                if (((Label) e.Row.FindControl("lblOriginalQty")) != null)
                {
                    this.TotalOriginalQty += decimal.Parse(((Label) e.Row.FindControl("lblOriginalQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblPreallocatedQty")) != null)
                {
                    this.TotalPreallocatedQty += decimal.Parse(((Label) e.Row.FindControl("lblPreallocatedQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblAllocatedQty")) != null)
                {
                    this.TotalAllcatedQty += decimal.Parse(((Label) e.Row.FindControl("lblAllocatedQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblPickedQty")) != null)
                {
                    this.TotalPickedQty += decimal.Parse(((Label) e.Row.FindControl("lblPickedQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblShippedQty")) != null)
                {
                    this.TotalShippedQty += decimal.Parse(((Label) e.Row.FindControl("lblShippedQty")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalOriginalQty.Text = base.GetPreferenceDigit(this.TotalOriginalQty);
                this.lblTotalPreallocatedQty.Text = base.GetPreferenceDigit(this.TotalPreallocatedQty);
                this.lblTotalAllcatedQty.Text = base.GetPreferenceDigit(this.TotalAllcatedQty);
                this.lblTotalPickedQty.Text = base.GetPreferenceDigit(this.TotalPickedQty);
                this.lblTotalShippedQty.Text = base.GetPreferenceDigit(this.TotalShippedQty);
            }
            if ((e.Row.RowType == DataControlRowType.Header) && !(string.IsNullOrEmpty(base.Request.QueryString["type"]) || !(base.Request.QueryString["type"] == "QUERY")))
            {
                ((HtmlImage) e.Row.FindControl("img01")).Visible = false;
            }
        }

        protected void gvOrderDetail_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            try
            {
                int index = e.NewSelectedIndex;
                if (!string.IsNullOrEmpty(this.gvOrderDetail.DataKeys[index]["Status"].ToString()))
                {
                    string lineID = this.gvOrderDetail.DataKeys[index]["LineID"].ToString();
                    List<OrderDetailInfo> orderDetailinfos = this.ViewState["OrderDetails"] as List<OrderDetailInfo>;
                    if ((orderDetailinfos != null) && (orderDetailinfos.Count > 0))
                    {
                        OrderDetailInfo orderDetailinfo = null;
                        for (int i = 0; i < orderDetailinfos.Count; i++)
                        {
                            if (orderDetailinfos[i].LineID.ToString() == lineID)
                            {
                                orderDetailinfo = orderDetailinfos[i];
                                break;
                            }
                        }
                        this.Session["Allocation"] = orderDetailinfo;
                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>popManualAllocation();</script>", false);
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_001");
                    this.lblMsg.Visible = true;
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4)</script>", false);
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
            }
        }

        protected void gvOrderDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                OrdersInfo ordersInfo = base.DataEntityViewState as OrdersInfo;
                DataView dv = CADataConverter.ToDataTable(ordersInfo.OrderDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvOrderDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);</script>", false);
            }
        }

        protected void gvPickDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7041", this.gvPickDetail);
        }

        protected void gvPickDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as OrdersInfo).PickDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',4,4) </script>", false);
        }

        protected void gvPickDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvPickDetail.PageIndex * this.gvPickDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.PDTotalQty += decimal.Parse(((Label) e.Row.FindControl("lblQty")).Text);
                }
                switch (this.gvPickDetail.DataKeys[e.Row.RowIndex]["Status"].ToString().ToUpper())
                {
                    case "NEW":
                    case "RELEASED":
                        ((TextBox) e.Row.FindControl("txtToLoc")).Enabled = true;
                        break;
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblPDTotalQty.Text = base.GetPreferenceDigit(this.PDTotalQty);
            }
        }

        protected void gvPickDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                OrdersInfo ordersInfo = base.DataEntityViewState as OrdersInfo;
                DataView dv = CADataConverter.ToDataTable(ordersInfo.PickDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvPickDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);</script>", false);
            }
        }

        protected void gvPreallocateDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7041", this.gvPreallocateDetail);
        }

        protected void gvPreallocateDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as OrdersInfo).PreallocatePickDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,4) </script>", false);
        }

        protected void gvPreallocateDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvPreallocateDetail.PageIndex * this.gvPreallocateDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                if (((Label) e.Row.FindControl("lblPreallocatedQty")) != null)
                {
                    this.PDTotalPreallocatedQty += decimal.Parse(((Label) e.Row.FindControl("lblPreallocatedQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblAllocatedQty")) != null)
                {
                    this.PDTotalAllocatedQty += decimal.Parse(((Label) e.Row.FindControl("lblAllocatedQty")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblPDTotalPreallocatedQty.Text = base.GetPreferenceDigit(this.PDTotalPreallocatedQty);
                this.lblPDTotalAllocatedQty.Text = base.GetPreferenceDigit(this.PDTotalAllocatedQty);
            }
        }

        protected void gvPreallocateDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                OrdersInfo ordersInfo = base.DataEntityViewState as OrdersInfo;
                DataView dv = CADataConverter.ToDataTable(ordersInfo.PreallocatePickDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvPreallocateDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,4);</script>", false);
            }
        }

        protected void lnkbtnAllocation_Click(object sernder, EventArgs e)
        {
            try
            {
                List<string> orderIDS = new List<string>();
                string orderDetailID = this.txtOrdersID.Text.Trim();
                orderIDS.Add(orderDetailID);
                string strMsg = string.Empty;
                strMsg = OutboundServiceClientProxy.GetInstance().AutoAllocate(orderIDS, base.UserInfo.UserID);
                if (strMsg.EndsWith(","))
                {
                    strMsg = strMsg.Substring(0, strMsg.Length - 1);
                }
                this.lblMsg.Text = strMsg;
                this.SetOrderInfoForm();
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnClosed_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> gInitLocal0 = new List<string> {
                    this.txtOrdersID.Text
                };
                string strMsg = OutboundServiceClientProxy.GetInstance().ClosedByOrderID(gInitLocal0);
                if (!string.IsNullOrEmpty(strMsg))
                {
                    this.lblMsg.Text = "出库单 " + strMsg + " 关闭成功!";
                    this.SetOrderInfoForm();
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void lnkbtnDirectAllocation_Click(object sender, EventArgs e)
        {
            try
            {
                string orderID = this.txtOrdersID.Text.Trim();
                List<string> lineID = base.GetSelectedIDsInGridView(this.gvOrderDetail, "LineID");
                if (!string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().DirectAllocation(orderID, lineID)))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_004") + orderID + "分配成功!";
                }
                else
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_006"), orderID);
                }
                this.SetOrderInfoForm();
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnOwnerTransfer_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() != "PARTPRE-ALLOCATED")
            {
                this.lblMsg.Text = "单据状态不等于预分配,无法进行货权转移!";
            }
            else
            {
                string orderDetailID = this.txtOrdersID.Text.Trim();
                List<string> lineID = base.GetSelectedIDsInGridView(this.gvOrderDetail, "LineID");
                if ((lineID == null) || (lineID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_017");
                }
                else
                {
                    try
                    {
                        string temp = OutboundServiceClientProxy.GetInstance().CreateOwnerTransferbyOrderIdLineID(orderDetailID, lineID);
                        if (!string.IsNullOrEmpty(temp))
                        {
                            this.lblMsg.Text = "生成建议货权转移成功!" + temp;
                        }
                        else
                        {
                            this.lblMsg.Text = "生成建议货权转移失败!";
                        }
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = "操作异常:" + ex.Message;
                    }
                }
            }
        }

        protected void lnkbtnPreAllocation_Click(object sernder, EventArgs e)
        {
            try
            {
                string orderDetailID = this.txtOrdersID.Text.Trim();
                string strMsg = string.Empty;
                strMsg = OutboundServiceClientProxy.GetInstance().AutoPreAllocate(orderDetailID, base.UserInfo.UserID);
                if (!string.IsNullOrEmpty(strMsg))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_004") + orderDetailID + strMsg;
                }
                else
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_005"), orderDetailID);
                }
                this.SetOrderInfoForm();
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnReleasePickTasks_Click(object sernder, EventArgs e)
        {
            try
            {
                List<string> pickDetailID = new List<string>();
                for (int i = 0; i < this.gvPickDetail.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvPickDetail.Rows[i].FindControl("chbSelect")).Checked && (this.gvPickDetail.DataKeys[i]["Status"].ToString().ToUpper() == "NEW"))
                    {
                        pickDetailID.Add(this.gvPickDetail.DataKeys[i]["PickDetailID"].ToString());
                    }
                }
                if ((pickDetailID == null) || (pickDetailID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_002");
                }
                else if (!string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().ReleasePickTasks(pickDetailID, base.UserInfo.UserID)))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_003");
                    this.SetOrderInfoForm();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnReleasePickTasksByNew_Click(object sernder, EventArgs e)
        {
            try
            {
                List<string> pickDetailID = new List<string>();
                for (int i = 0; i < this.gvPickDetail.Rows.Count; i++)
                {
                    if (this.gvPickDetail.DataKeys[i]["Status"].ToString().ToUpper() == "NEW")
                    {
                        pickDetailID.Add(this.gvPickDetail.DataKeys[i]["PickDetailID"].ToString());
                    }
                }
                if ((pickDetailID == null) || (pickDetailID.Count == 0))
                {
                    this.lblMsg.Text = "没有可打印的拣货记录!";
                }
                else if (!string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().ReleasePickTasks(pickDetailID, base.UserInfo.UserID)))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_003");
                    this.SetOrderInfoForm();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnRevertAllocation_Click(object sernder, EventArgs e)
        {
            try
            {
                string orderDetailID = this.txtOrdersID.Text.Trim();
                string strMsg = OutboundServiceClientProxy.GetInstance().AutoRevertAllocate(orderDetailID);
                if (!string.IsNullOrEmpty(strMsg))
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_009"), strMsg.Substring(0, strMsg.Length - 1));
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_010");
                }
                this.SetOrderInfoForm();
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnRevertPreAllocation_Click(object sernder, EventArgs e)
        {
            try
            {
                string orderDetailID = this.txtOrdersID.Text.Trim();
                string strMsg = string.Empty;
                strMsg = OutboundServiceClientProxy.GetInstance().AutoRevertPreAllocate(orderDetailID);
                if (!string.IsNullOrEmpty(strMsg))
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7400_007"), orderDetailID, strMsg.Substring(0, strMsg.Length - 1));
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7400_008");
                }
                this.SetOrderInfoForm();
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlType, "OrderType");
            base.BindData2DDLBySysCode(this.ddlStatus, "OrderStatus");
            base.BindData2DDL4UserPermissionWareHouse(this.ddlWareHouseID);
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnOrdersID.Value = base.Request.QueryString["ID"].ToString();
                this.SetOrderInfoForm();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["type"]) && (base.Request.QueryString["type"] == "QUERY"))
            {
                this.btnBatchAllocate.Visible = false;
                this.btnBatchPreAllocate.Visible = false;
                this.btnBatchRevertAllocate.Visible = false;
                this.btnBatchRevertPreAllocate.Visible = false;
                this.btnConfirmPick.Visible = false;
                this.btnGoBack.Visible = false;
                this.btnRefresh.Visible = false;
                this.btnRevertPick.Visible = false;
                this.btnSave.Visible = false;
                this.htmlbtnAction.Visible = false;
                this.htmlbtnReport.Visible = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetOrderInfoForm()
        {
            try
            {
                string orderID = this.hdnOrdersID.Value.ToString();
                OrdersInfo ordersInfo = OutboundServiceClientProxy.GetInstance().GetOrdersInfoByOrderID(orderID);
                base.DataEntityViewState = ordersInfo;
                if (ordersInfo != null)
                {
                    this.txtActualShipDate.Text = base.GetPreferenceShortDate(ordersInfo.ActualShipDate);
                    this.txtDeliverDate.Text = base.GetPreferenceShortDate(ordersInfo.DeliveryDate);
                    this.txtExternalOrderID.Text = ordersInfo.ExternOrderID;
                    this.txtExternalOrderID2.Text = ordersInfo.ExternalOrderId2;
                    this.txtOrdersDate.Text = base.GetPreferenceShortDate(ordersInfo.OrderDate);
                    this.txtOrdersID.Text = ordersInfo.OrderID;
                    this.txtOwernID.Text = ordersInfo.OwnerID;
                    this.hdnOwnerID.Value = ordersInfo.OwnerID;
                    this.txtPriority.Text = ordersInfo.Priority.ToString();
                    this.txtReferenceNum.Text = ordersInfo.ReferenceNum;
                    this.txtRequestedShipDate.Text = base.GetPreferenceShortDate(ordersInfo.RequestedShipDate);
                    this.ddlStatus.SelectedValue = ordersInfo.Status;
                    this.txtOrderGroup.Text = ordersInfo.OrderGroup;
                    this.ddlType.SelectedValue = ordersInfo.Type;
                    this.txtConsigneeID.Text = ordersInfo.ConsigneeID;
                    this.txtCCompany.Text = ordersInfo.CCompany;
                    if (!string.IsNullOrEmpty(ordersInfo.IntransitID) && (this.ddlWareHouseID.Items.Count > 0))
                    {
                        this.ddlWareHouseID.SelectedValue = ordersInfo.DestWhDbID;
                    }
                    this.txtIntransitID.Text = ordersInfo.IntransitID;
                    this.ddlWareHouseID.Enabled = false;
                    this.txtIntransitID.Enabled = false;
                    this.ddlType_SelectedIndexChanged(null, null);
                    base.BindData2GridView(this.gvOrderDetail, ordersInfo.OrderDetailInfos);
                    this.ViewState["OrderDetails"] = ordersInfo.OrderDetailInfos;
                    base.BindData2GridView(this.gvPreallocateDetail, ordersInfo.PreallocatePickDetailInfos);
                    base.BindData2GridView(this.gvPickDetail, ordersInfo.PickDetailInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

